package com.infovalley.info_valley.wxapi;

import android.app.Activity;
import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.util.Log;

import com.infovalley.tx_plugin.PluginConstants;
import com.tencent.mm.opensdk.constants.ConstantsAPI;
import com.tencent.mm.opensdk.modelbase.BaseReq;
import com.tencent.mm.opensdk.modelbase.BaseResp;
import com.tencent.mm.opensdk.modelmsg.SendAuth;
import com.tencent.mm.opensdk.modelmsg.ShowMessageFromWX;
import com.tencent.mm.opensdk.modelmsg.WXMediaMessage;
import com.tencent.mm.opensdk.openapi.IWXAPI;
import com.tencent.mm.opensdk.openapi.IWXAPIEventHandler;
import com.tencent.mm.opensdk.openapi.WXAPIFactory;

/**
 * 微信刷新的回调
 */
public class WXEntryActivity extends Activity implements IWXAPIEventHandler {
    private static final String TAG = "Nick";

    private IWXAPI api;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        Log.d(TAG, "wx callback---onCreate");
        api = WXAPIFactory.createWXAPI(this, PluginConstants.getWxAppID(), false);
        try {
            api.handleIntent(getIntent(), this);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    @Override
    protected void onNewIntent(Intent intent) {
        super.onNewIntent(intent);
        Log.d(TAG, "wx callback---onNewIntent");
        setIntent(intent);
        api.handleIntent(intent, this);
    }

    @Override
    public void onReq(BaseReq req) {
        Log.d(TAG, "微信的回调---onReq");
        if (req.getType() == ConstantsAPI.COMMAND_SHOWMESSAGE_FROM_WX && req instanceof ShowMessageFromWX.Req) {
            ShowMessageFromWX.Req showReq = (ShowMessageFromWX.Req) req;
            WXMediaMessage mediaMsg = showReq.message;
            String extInfo = mediaMsg.messageExt;//网页获取的参数
            Log.d(TAG, "微信的参数---extInfo"+extInfo);
            if (extInfo!=null&&!extInfo.isEmpty()){
                Intent extInfoIntent = new Intent(PluginConstants.WX_JUMPAPP_MESSAGE);
                extInfoIntent.putExtra(PluginConstants.EXTINFO,extInfo);
                sendBroadcast(extInfoIntent);
            }
            Intent intent = new Intent(Intent.ACTION_VIEW);
            intent.putExtra(PluginConstants.EXTINFO,extInfo);
            intent.setData(Uri.parse("xinxigu://"));
            startActivity(intent);


        }
        Log.d(TAG, "微信的finish---finish");
        this.finish();
    }
    @Override
    public void onResp(BaseResp resp) {
        Log.d(TAG, "wx callback---onResp---" + resp.errCode);
        switch (resp.getType()) {
            case ConstantsAPI.COMMAND_SENDAUTH:
                Log.d(TAG, "onResp---user authorization callback: COMMAND_SEND_AUTH");
                SendAuth.Resp authResp = (SendAuth.Resp) resp;
                final String code = authResp.code;
                Intent intent = new Intent(PluginConstants.WX_AUTHORIZATION_FILTER);
                intent.putExtra("code", code);
                sendBroadcast(intent);
                break;
            case ConstantsAPI.COMMAND_SENDMESSAGE_TO_WX:
                Log.d(TAG, "onResp---shared callback: COMMAND_SEND_MESSAGE_TO_WX");
                Intent sharedIntent = new Intent(PluginConstants.WX_SHARED_FILTER);
                sharedIntent.putExtra("errCode", resp.errCode);
                sendBroadcast(sharedIntent);
                break;
            case ConstantsAPI.COMMAND_SUBSCRIBE_MESSAGE:
                Log.d(TAG, "onResp: COMMAND_SUBSCRIBE_MESSAGE");

                //SubscribeMessage.Resp subscribeMsgResp = (SubscribeMessage.Resp) resp;
                //String text = String.format("openid=%s\ntemplate_id=%s\nscene=%d\naction=%s\nreserved=%s",
                //subscribeMsgResp.openId, subscribeMsgResp.templateID, subscribeMsgResp.scene,
                //subscribeMsgResp.action, subscribeMsgResp.reserved);
                break;
            case ConstantsAPI.COMMAND_LAUNCH_WX_MINIPROGRAM:
                Log.d(TAG, "onResp: COMMAND_LAUNCH_WX_MINIPROGRAM");
                //WXLaunchMiniProgram.Resp launchMiniProgramResp = (WXLaunchMiniProgram.Resp) resp;
                //String text = String.format("openid=%s\nextMsg=%s\nerrStr=%s",
                //launchMiniProgramResp.openId,
                //launchMiniProgramResp.extMsg,launchMiniProgramResp.errStr);
                break;

            case ConstantsAPI.COMMAND_OPEN_BUSINESS_VIEW:
                Log.d(TAG, "onResp: COMMAND_OPEN_BUSINESS_VIEW");
                //WXOpenBusinessView.Resp launchMiniProgramResp = (WXOpenBusinessView.Resp) resp;
                //String text = String.format("openid=%s\nextMsg=%s\nerrStr=%s\nbusinessType=%s",
                //launchMiniProgramResp.openId, launchMiniProgramResp.extMsg,
                //launchMiniProgramResp.errStr,launchMiniProgramResp.businessType);
                break;
            case ConstantsAPI.COMMAND_OPEN_BUSINESS_WEBVIEW:
                Log.d(TAG, "onResp: COMMAND_OPEN_BUSINESS_WEBVIEW");
                //WXOpenBusinessWebview.Resp response = (WXOpenBusinessWebview.Resp) resp;
                //String text = String.format("businessType=%d\nresultInfo=%s\nret=%d",
                //response.businessType,response.resultInfo,response.errCode);
                break;
            default:
                break;
        }
        finish();
    }
}